home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / libx11 / libx11 / pixmaps.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  14KB  |  471 lines

  1. /* Copyright (c) 1996 by Terje Pedersen.  All Rights Reserved   */
  2. /*                                                              */
  3. /* By using this code you will agree to these terms:            */
  4. /*                                                              */
  5. /* 1. You may not use this code for profit in any way or form   */
  6. /*    unless an agreement with the author has been reached.     */
  7. /*                                                              */
  8. /* 2. The author is not responsible for any damages caused by   */
  9. /*    the use of this code.                                     */
  10. /*                                                              */
  11. /* 3. All modifications are to be released to the public.       */
  12. /*                                                              */
  13. /* Thats it! Have fun!                                          */
  14. /* TP                                                           */
  15. /*                                                              */
  16.  
  17. /***
  18.    NAME
  19.      pixmaps
  20.    PURPOSE
  21.      add pixmap handling to libX11
  22.    NOTES
  23.      
  24.    HISTORY
  25.      Terje Pedersen - Oct 27, 1994: Created.
  26. ***/
  27.  
  28. #include <amiga.h>
  29. #include <stdio.h>
  30.  
  31. #include "libX11.h"
  32. #define XLIB_ILLEGAL_ACCESS 1
  33.  
  34. #include <X11/X.h>
  35. #include <X11/Xlib.h>
  36. #include <X11/Xutil.h>
  37.  
  38. #include "x11display.h"
  39. #include "images.h"
  40.  
  41. #ifdef DEBUGXEMUL_ENTRY
  42. extern int bInformImages; /* ignore outputting information about images */
  43. extern int bSkipImageWrite;
  44. #endif
  45.  
  46. /*******************************************************************************************/
  47.  
  48. /********************************************************************************
  49. Name     : XCreatePixmap()
  50. Author   : Terje Pedersen
  51. Input    : 
  52.      display   Specifies  a  connection  to  an  X  server;  returned  from
  53.                XOpenDisplay().
  54.  
  55.      drawable  Specifies the drawable.  May be an InputOnly window.
  56.  
  57.      width
  58.      height
  59.                Specify the width and height in pixels of the  pixmap.   The
  60.                values must be nonzero.
  61.  
  62.      depth     Specifies the depth  of  the  pixmap.   The  depth  must  be
  63.                supported  by  the  screen  of the specified drawable.  (Use
  64.                XListDepths() if in doubt.)
  65.  
  66. Output   : 
  67. Function : create a pixmap.
  68. ********************************************************************************/
  69.  
  70. Pixmap
  71. XCreatePixmap( Display* display,
  72.            Drawable drawable,
  73.            unsigned int width,
  74.            unsigned int height,
  75.            unsigned int depth )
  76. {
  77.   struct BitMap * bitmap;
  78.   Pixmap vPixmap;
  79.  
  80.   assert(X11DrawablesBackground);
  81.  
  82. #ifdef DEBUGXEMUL_ENTRY
  83.   FunCount_Enter( XCREATEPIXMAP , bInformImages );
  84. #endif
  85.   bitmap = alloc_bitmap( width, height, depth, BMF_CLEAR, DG.wb->RastPort.BitMap );
  86.   if( DG.XAllocFailed )
  87.     return NULL;
  88.  
  89.   vPixmap = (Pixmap)X11NewBitmap(bitmap,width,height,depth);
  90.   if( drawable==DG.X11Screen[0].root || drawable==ROOTID ){
  91.     drawable = 0;
  92.   }
  93.   X11DrawablesBackground[vPixmap] = X11DrawablesBackground[drawable];
  94.   return(vPixmap);
  95. }
  96.  
  97. /********************************************************************************
  98. Name     : XFreePixmap()
  99. Author   : Terje Pedersen
  100. Input    : 
  101.      display   Specifies  a  connection  to  an  X  server;  returned  from
  102.                XOpenDisplay().
  103.  
  104.      pixmap    Specifies the pixmap whose ID should be freed.
  105.  
  106. Output   : 
  107. Function : free a pixmap ID.
  108. ********************************************************************************/
  109.  
  110. XFreePixmap( Display* display, Pixmap pixmap )
  111. {
  112.   struct BitMap *bitmap = X11DrawablesBitmaps[X11DrawablesMap[pixmap]].pBitMap;
  113.  
  114. #ifdef DEBUGXEMUL_ENTRY
  115.   FunCount_Enter( XFREEPIXMAP, bInformImages );
  116. #endif
  117.  
  118.   assert( bitmap->Depth>0 );
  119.  
  120.   if( X11DrawablesBitmaps[X11DrawablesMap[pixmap]].bTileStipple ){
  121.     X11DrawablesBitmaps[X11DrawablesMap[pixmap]].bTileStipple |= BITMAP_DELETED;
  122.     return;
  123.   }
  124.  
  125.   if( DG.oldX11FillSource == pixmap )
  126.     DG.oldX11FillSource = -1;
  127.  
  128.   X11FreeBitmap( pixmap );
  129.   free_bitmap( bitmap );
  130.   X11DrawablesBitmaps[X11DrawablesMap[pixmap]].pBitMap = NULL;
  131.  
  132.   return(0);
  133. }
  134.  
  135. /********************************************************************************
  136. Name     : XCreatePixmapFromBitmapData()
  137. Author   : Terje Pedersen
  138. Input    : 
  139.      display   Specifies a connection to  an  Display  structure,  returned
  140.                from XOpenDisplay().
  141.  
  142.      drawable  Specifies a drawable ID which  indicates  which  screen  the
  143.                pixmap is to be used on.
  144.  
  145.      data      Specifies the data in bitmap format.
  146.  
  147.      width
  148.      height
  149.                Specify the width and height in  pixels  of  the  pixmap  to
  150.                create.
  151.  
  152.      fg
  153.      bg
  154.                Specify the foreground and background pixel values to use.
  155.  
  156.      depth     Specifies the depth of the pixmap.  Must  be  valid  on  the
  157.                screen specified by drawable.
  158.  
  159. Output   : 
  160. Function : create a pixmap with depth from bitmap data.
  161. ********************************************************************************/
  162.  
  163. Pixmap
  164. XCreatePixmapFromBitmapData( Display* display,
  165.                  Drawable drawable,
  166.                  char* data,
  167.                  unsigned int width,
  168.                  unsigned int height,
  169.                  unsigned long fg,
  170.                  unsigned long bg,
  171.                  unsigned int depth )
  172. {
  173.   Pixmap pm;
  174. #if 1
  175.   Pixmap pm2;
  176.   struct BitMap *bm;
  177.   struct BitMap *bitmap;
  178.   int clear = 1;
  179. /*  int x,y,bpl;*/
  180. #endif
  181.  
  182.   assert(X11DrawablesBackground);
  183.  
  184. #ifdef DEBUGXEMUL_ENTRY
  185.   FunCount_Enter( XCREATEPIXMAPFROMBITMAPDATA , bInformImages );
  186. #endif
  187. #if 1
  188.   pm = XCreatePixmap(display,drawable,width,height,depth);
  189.   bm = X11DrawablesBitmaps[X11DrawablesMap[pm]].pBitMap;
  190.   pm2 = XCreateBitmapFromData(display,drawable,data,width,height);
  191.   bitmap = X11DrawablesBitmaps[X11DrawablesMap[pm2]].pBitMap;
  192. /*
  193.   bitmap = alloc_bitmap( width, height, 1, BMF_CLEAR, DG.wb->RastPort.BitMap );
  194.   if( DG.XAllocFailed ) return NULL;
  195.  
  196.   bpl = (width+7)>>3;
  197.   for( y=0; y<height; y++ )
  198.     for( x=0; x<bpl; x++ )
  199.       *(bitmap->Planes[0]+y*bitmap->BytesPerRow+x) = X11InvertMap[*(data+y*bpl+x)];
  200. */
  201.   DG.X11BitmapRP.BitMap = bm;
  202.  
  203.   vPrevGC=(GC)-1;
  204.  
  205.   
  206.   if( clear ){
  207.     SetAPen(&DG.X11BitmapRP,bg % (1<<depth) /*X11DrawablesBackground[drawable]*/);
  208.     RectFill(&DG.X11BitmapRP,0,0,width-1,height-1);
  209.   }
  210.   SetDrMd(&DG.X11BitmapRP,JAM1);
  211.   SetAPen(&DG.X11BitmapRP, fg % (1<<depth));
  212.   SetBPen(&DG.X11BitmapRP, bg % (1<<depth));
  213.  
  214. #if 0
  215.   BltTemplate(bitmap->Planes[0],0,bitmap->BytesPerRow,&DG.X11BitmapRP,0,0,width,height);
  216.   SetAPen(&DG.X11BitmapRP,1);
  217. #endif
  218.   WaitBlit();
  219.   BltPattern(&DG.X11BitmapRP,bitmap->Planes[0],0,0,width-1,height-1,bitmap->BytesPerRow);
  220.   WaitBlit();
  221. #if (DEBUG!=0)
  222.   if(show) showbitmap(DG.X11BitmapRP.BitMap,width,height,0,2);
  223. #endif
  224.   XFreePixmap(NULL,pm2);
  225. /*
  226.   free_bitmap(bitmap);
  227. */
  228. #else
  229.   if( depth==8 ){
  230.     struct BitMap *bm;
  231.  
  232.     pm = XCreatePixmap(display,drawable,width,height,depth);
  233.     bm = X11DrawablesBitmaps[X11DrawablesMap[pm]].pBitMap;
  234.     memcpy(bm->Planes[0],data,bm->BytesPerRow*bm->Rows);
  235.   } else
  236.     return(XCreateBitmapFromData(display,drawable,data,width,height));
  237. #endif
  238.  
  239.   return(pm);
  240. }
  241.  
  242. /********************************************************************************
  243. Name     : 
  244. Author   : Terje Pedersen
  245. Input    : 
  246. Output   : 
  247. Function : 
  248. ********************************************************************************/
  249.  
  250. /********************************************************************************
  251. Name     : XCreateBitmapFromData()
  252. Author   : Terje Pedersen
  253. Input    : 
  254.      display   Specifies  a  connection  to  an  X  server;  returned  from
  255.                XOpenDisplay().
  256.  
  257.      drawable  Specifies a  drawable.   This  determines  which  screen  to
  258.                create the bitmap on.
  259.  
  260.      data      Specifies the bitmap data, in X11 bitmap file format.
  261.  
  262.      width
  263.      height
  264.                Specify the dimensions in pixels of the created bitmap.   If
  265.                smaller  than  the bitmap data, the upper-left corner of the
  266.                data is used.
  267.  
  268. Output   : 
  269. Function : create a bitmap from X11 bitmap format data.
  270. ********************************************************************************/
  271.  
  272. UBYTE
  273. X11invert2( UBYTE n )
  274. {
  275.   return((UBYTE)(((n&128)>>7)+((n&64)>>5)+((n&32)>>3)+((n&16)>>1)+((n&8)<<1)+((n&4)<<3)+((n&2)<<5)+((n&1)<<7)));
  276. }
  277.  
  278.  
  279. Pixmap
  280. XCreateBitmapFromData( Display* display,
  281.                Drawable drawable,
  282.                char* data,
  283.                unsigned int width,
  284.                unsigned int height )
  285. {
  286.   Pixmap pm;
  287.   int bytes,i,bpl,bdpl;
  288.   struct BitMap *bm;
  289.  
  290. #ifdef DEBUGXEMUL_ENTRY
  291.   FunCount_Enter( XCREATEBITMAPFROMDATA , bInformImages );
  292. #endif
  293.   pm = XCreatePixmap(display,drawable,width,height,1);
  294.   bm = X11DrawablesBitmaps[X11DrawablesMap[pm]].pBitMap;
  295.  
  296.   assert( bm->Depth>0 );
  297.  
  298.   bdpl = (width+7)>>3;
  299.   bytes= bdpl*height;
  300.   bpl = ((width+15)>>4)<<1;
  301.   for( i=0; i<bytes; i++ ){
  302.     *(bm->Planes[0]+(i%bdpl)+(int)(i/bdpl)*bpl) = (int)X11InvertMap[(int)data[i]];
  303.     assert( X11InvertMap[(int)data[i]] == X11invert2(data[i]) );
  304.     assert( X11InvertMap[*(bm->Planes[0]+(i%bdpl)+(int)(i/bdpl)*bpl)] == (int)data[i] );
  305.   }
  306. #if (DEBUG!=0)
  307.   if(show) showbitmap(bm,width,height,0,2);
  308. #endif
  309.  
  310.   return(pm);
  311. }
  312.  
  313. /********************************************************************************
  314. Name     : XReadBitmapFile()
  315. Author   : Terje Pedersen
  316. Input    : 
  317.      display        Specifies a connection to an X  server;  returned  from
  318.                     XOpenDisplay().
  319.  
  320.      d              Specifies the drawable.
  321.  
  322.      filename       Specifies the filename  to  use.   The  format  of  the
  323.                     filename is operating system specific.
  324.  
  325.      width_return
  326.      height_return
  327.                     Return the dimensions in pixels of the bitmap  that  is
  328.                     read.
  329.  
  330.      bitmap_return  Returns the pixmap resource ID that is created.
  331.  
  332.      x_hot_return
  333.      y_hot_return
  334.                     Return the hotspot coordinates in the file (or  - 1,  -
  335.                     1 if none present).
  336.  
  337. Output   : 
  338. Function : read a bitmap from disk.
  339. ********************************************************************************/
  340.  
  341. int
  342. XReadBitmapFile( Display* display,
  343.          Drawable d,
  344.          char* filename,
  345.          unsigned int* width_return,
  346.          unsigned int* height_return,
  347.          Pixmap* bitmap_return,
  348.          int* x_hot_return,
  349.          int* y_hot_return )
  350. {
  351.   char line[256],line2[256];
  352.   FILE *fp;
  353.   struct BitMap *bm;
  354.   int v,i,bytes,c,/*swap=0,*/perline,base;
  355.  
  356. #ifdef DEBUGXEMUL_ENTRY
  357.   FunCount_Enter( XREADBITMAPFILE, bInformImages );
  358. #endif
  359.   fp = fopen(filename,"r+");
  360.   if( !fp )
  361.     return(1);
  362.   fgets(line,256,fp); sscanf(line,"%s %s %d",line2,line2,width_return);
  363.   fgets(line,256,fp); sscanf(line,"%s %s %d",line2,line2,height_return);
  364.   fgets(line,256,fp);
  365.   if( line[0]=='#' ){
  366.     sscanf(line,"%s %s %d",line2,line2,x_hot_return);
  367.     fgets(line,256,fp);
  368.     sscanf(line,"%s %s %d",line2,line2,y_hot_return);
  369.     fgets(line,256,fp);
  370.   }
  371.   *bitmap_return = XCreatePixmap(display,d,*width_return,*height_return,1);
  372.   perline = ((*width_return)+7)>>3;
  373.   bytes = perline*(*height_return);
  374. /*  if(*width_return>8) swap=1;*/
  375.   if( perline&1 )
  376.     base = perline+1;
  377.   else
  378.     base = perline;
  379.   bm = X11DrawablesBitmaps[X11DrawablesMap[*bitmap_return]].pBitMap;
  380.   
  381.   for( i=0; i<bytes; i++ ){
  382.     UBYTE *b = bm->Planes[0];
  383.  
  384.     fscanf(fp,"%x%c",&v,&c);
  385.     *(b+(int)(i/perline)*base+i%perline) = X11InvertMap[(UBYTE)v];
  386.   }
  387.   *width_return = (((*width_return)+7)>>3)<<3;
  388.   fclose(fp);
  389.  
  390.   return(0);
  391. }
  392.  
  393. /********************************************************************************
  394. Name     : XWriteBitmapFile()
  395. Author   : Terje Pedersen
  396. Input    : 
  397.      display   Specifies  a  connection  to  an  X  server;  returned  from
  398.                XOpenDisplay().
  399.  
  400.      filename  Specifies the filename to use.  The format of  the  filename
  401.                is operating system specific.
  402.  
  403.      bitmap    Specifies the bitmap to be written.
  404.  
  405.      width
  406.      height
  407.                Specify the width and height in pixels of the bitmap  to  be
  408.                written.
  409.  
  410.      x_hot
  411.      y_hot
  412.                Specify where to place the hotspot coordinates (or  - 1, - 1
  413.                if none present) in the file.
  414.  
  415. Output   : 
  416. Function : write a bitmap to a file.
  417. ********************************************************************************/
  418.  
  419. int
  420. XWriteBitmapFile( Display* display,
  421.           char* filename,
  422.           Pixmap bitmap,
  423.           unsigned int width,
  424.           unsigned int height,
  425.           int x_hot,
  426.           int y_hot )
  427. {
  428.   char outname[40];
  429.   FILE *fptr;
  430.   int i,j,k=0;
  431.   struct BitMap *bm = X11DrawablesBitmaps[X11DrawablesMap[bitmap]].pBitMap;
  432.   unsigned char *outdata = bm->Planes[0];
  433.  
  434. #ifdef DEBUGXEMUL_ENTRY
  435.   FunCount_Enter( XWRITEBITMAPFILE, bInformImages );
  436. #endif
  437.   if( strchr(filename,'/') )
  438.     strcpy(outname,strrchr(filename,'/')+1);
  439.   else
  440.     if( strchr(filename,':') )
  441.       strcpy(outname,strrchr(filename,':')+1);
  442.     else
  443.       strcpy(outname,filename);
  444.   if( strchr(outname,'.') )
  445.     *strchr(outname,'.')=0;
  446.   fptr = fopen(filename,"w+");
  447.   if( !fptr )
  448.     return(BitmapOpenFailed);
  449.   fprintf(fptr,"#define %s_width %d\n",outname,width);
  450.   fprintf(fptr,"#define %s_height %d\n",outname,height);
  451.   fprintf(fptr,"#define %s_x_hot %d\n",outname,x_hot);
  452.   fprintf(fptr,"#define %s_y_hot %d\n",outname,y_hot);
  453.   fprintf(fptr,"static char %s_bits[] = {\n",outname);
  454.   for( i=0; i<height; i++ ){
  455.     for( j=0; j<(width+7)>>3; j++ ){
  456.       unsigned char c = X11InvertMap[(UBYTE)(*(outdata+i*bm->BytesPerRow+j))];
  457.  
  458.       fprintf(fptr,"0x%02x, ",c);
  459.       k++;
  460.       if( k==12 ){
  461.     fprintf(fptr,"\n");
  462.     k=0;
  463.       }
  464.     }
  465.   }
  466.   fprintf(fptr,"};");
  467.   fclose(fptr);
  468.  
  469.   return(BitmapSuccess);
  470. }
  471.